<?php
    require_once('../code/global.php');
    if(isset($_POST['action'])){
        //管理员登录
        if($_POST['action']=='adminLogin'){
            require_once('/code/Admin.php');
            $admin=new Admin();
            $admin->login($_POST['admin'], $_POST['password']);
        }
        //管理员注销
        if($_POST['action']=='adminLogout'){
            require_once('/code/Admin.php');
            $admin=new Admin();
            $admin->logout();
        }
        //修改密码
        if($_POST['action']=='editPwd'){
            require_once('/code/Admin.php');
            $returnUrl='accountInfo.html';
            $username=$_COOKIE['admin'];
            $oldPwd=$_POST['oldPwd'];
            $newPwd=$_POST['newPwd'];  
            $repeatPwd=$_POST['repeatPwd'];
            $admin=new Admin();
            if($newPwd!=$repeatPwd){
                header('Location:/admin/'.$returnUrl.'?msg=两次输入的密码不一致');
            }
            elseif(!$admin->pwdCheck($username, $oldPwd)){
                header('Location:/admin/'.$returnUrl.'?msg=原密码不正确');
            }
            else{
                if($admin->editPwd($username, $newPwd, $oldPwd)) {header('Location:/admin/'.$returnUrl.'?msg=密码修改成功');}
                else {header('Location:/admin/'.$returnUrl.'?msg=密码未作修改');}
            }
        }
        //编辑分类
        if($_POST['action']=='editCate'){
            require_once('/code/Category.php');
            $c['category_id']=$_POST['category_id'];
            $c['name']=$_POST['name'];
            $c['sequence_num']=intval($_POST['sequence_num']);
            $c['is_active']=$_POST['is_active']=='on'?1:0;
            $category=new Category();
            if($category->editCategory($c)) {header('Location:/admin/category.html?msg=分类修改成功');}
            else {header('Location:/admin/category.html?msg=分类未作修改');}
        }
        //添加分类
        if($_POST['action']=='addCate'){
            if($_POST['name']=='' || $_POST['sequence_num']=='') header('Location:/admin/category.html?msg=分类添加失败');
            else{
                require_once('/code/Category.php');
                $c['name']=$_POST['name'];
                $c['sequence_num']=intval($_POST['sequence_num']);
                $c['is_active']=$_POST['is_active']=='on'?1:0;
                $category=new Category();
                if($category->addCategory($c)) {header('Location:/admin/category.html?msg=分类添加成功');}
                else {header('Location:/admin/category.html?msg=分类添加失败');}
            }
        }
        //删除分类
        if($_POST['action']=='deleteCate'){
            require_once('/code/Category.php');
            $cid=$_POST['category_id'];
            $category=new Category();
            $rs=$category->deleteCategory($cid);
            if($rs>0) {header('Location:/admin/category.html?msg=分类删除成功');}
            elseif($rs==-1) {header('Location:/admin/category.html?msg=不能删除，该分类下仍有照片');}
            else {header('Location:/admin/category.html?msg=分类删除失败');}
        }
        //编辑摄影师
        if($_POST['action']=='editArtist'){
            require_once('/code/Artist.php');
            $a['artist_id']=$_POST['artist_id'];
            $a['name']=$_POST['name'];
            $a['is_active']=$_POST['is_active']=='on'?1:0;
            $artist=new Artist();
            if($artist->editArtist($a)) {header('Location:/admin/photographer.html?msg=摄影师修改成功');}
            else {header('Location:/admin/photographer.html?msg=摄影师修改失败');}
        }
        //添加摄影师
        if($_POST['action']=='addArtist'){
            if($_POST['name']=='') header('Location:/admin/photographer.html?msg=请填写摄影师名字');
            else{
                require_once('/code/Artist.php');
                $a['name']=$_POST['name'];
                $a['is_active']=$_POST['is_active']=='on'?1:0;
                $artist=new Artist();
                if($artist->addArtist($a)) {header('Location:/admin/photographer.html?msg=摄影师添加成功');}
                else {header('Location:/admin/photographer.html?msg=摄影师添加失败');}
            }
        }
        //删除摄影师
        if($_POST['action']=='deleteArtist'){
            require_once('/code/Artist.php');
            $aid=$_POST['artist_id'];
            $artist=new Artist();
            $rs=$artist->deleteArtist($aid);
            if($rs>0) {header('Location:/admin/photographer.html?msg=摄影师删除成功');}
            elseif($rs==-1) {header('Location:/admin/photographer.html?msg=不能删除，该摄影师仍有作品');}
            else {header('Location:/admin/photographer.html?msg=摄影师删除失败');}
        }
        //编辑照片信息
        if($_POST['action']=='editPhoto'){
            require_once('/code/Product.php');
            $p['product_id']=$_POST['product_id'];
            $p['category_id']=$_POST['category_id'];
            $p['subject']=$_POST['subject'];
            $p['type']=$_POST['type'];
            $p['date']=$_POST['date'];
            $p['artist_id']=$_POST['artist_id'];
            $p['code']=$_POST['code'];
            $p['area']=$_POST['area'];
            $p['condition']=$_POST['condition'];
            $p['size']=$_POST['size'];
            $p['price']=doubleval($_POST['price']);
            $p['is_active']=$_POST['is_active']=='on'?1:0;
            $p['is_sold']=$_POST['is_sold']=='on'?1:0;
            $product=new Product();
            if($product->editProduct($p)) header('Location:/admin/photo.html?cid='.$p['category_id'].'&msg=照片信息已保存');
            else header('Location:/admin/photo.html?cid='.$p['category_id'].'&msg=照片信息未修改');
        }
        //添加照片
        if($_POST['action']=='addPhoto'){
            $p['category_id']=$_POST['category_id'];
            if($_FILES['imgFile']['error']==0){
                require_once('/code/Product.php');
                $imgFile=&$_FILES['imgFile'];
                $product=new Product();
                try{
                    $pid=$product->createProduct($p,$imgFile);
                    if($pid!=0) header('Location:/admin/editPhoto.html?pid='.$pid);
                    else header('Location:/admin/photo.html?msg=照片创建失败');
                } 
                catch(Exception $e){header('Location:/admin/photo.html?cid='.$p['category_id'].'&msg='.$e->getMessage());}
            }
            else header('Location:/admin/photo.html?cid='.$p['category_id'].'&msg=未选择图片，或图片文件大于10M');
        }
        //删除照片
        if($_POST['action']=='deletePhoto'){
            require_once('/code/Product.php');
            $pid=$_POST['product_id'];
            $cid=$_POST['category_id'];
            $product=new Product();
            if($product->deleteProduct($pid)) header('Location:/admin/photo.html?cid='.$cid.'&msg=照片已删除');
            else header('Location:/admin/photo.html?cid='.$cid.'&msg=删除照片失败');
        }
        //网站信息设置
        if($_POST['action']=='editSiteInfo'){
            require_once('/code/SiteInfo.php');
            $s['site_name']=$_POST['site_name'];
            $s['meta_keyword']=$_POST['meta_keyword'];
            $s['meta_description']=$_POST['meta_description'];
            $s['site_about']=$_POST['site_about'];
            $s['contact_email']=$_POST['contact_email'];
            $s['address']=$_POST['address'];
            $s['terms_condition']=$_POST['terms_condition'];
            $s['frontpage_artist_id']=$_POST['frontpage_artist_id'];
            $s['frontpage_photo_subject']=$_POST['frontpage_photo_subject'];
            $s['watermark_text']=$_POST['watermark_text'];
            $s['watermark_transparency']=$_POST['watermark_transparency'];
            $s['watermark_fontsize_img']=intval($_POST['watermark_fontsize_img']);
            $s['watermark_fontsize_thumb']=intval($_POST['watermark_fontsize_thumb']);
            $siteInfo=new SiteInfo();
            $siteInfo->editSiteInfo($s);
            if($siteInfo->editSiteInfo($s)) header('Location:/admin/siteInfo.html?msg=网站设置已保存');
            else header('Location:/admin/siteInfo.html?msg=网站设置未修改');
        }
        //网站设置，上传首页照片
        if($_POST['action']=='uploadHomeImg'){
            if($_FILES['imgFile']['error']==0){
                require_once('/code/SiteInfo.php');
                $siteInfo=new SiteInfo();
                try{
                    $siteInfo->uploadImg('.'.$siteInfo->HOMEPAGE_IMG_URL, $_FILES['imgFile']['tmp_name']);
                    header('Location:/admin/siteInfo.html?nid='.$nid.'&msg=首页照片上传成功');
                }
                catch(Exception $e){header('Location:/admin/siteInfo.html?msg='.$e->getMessage());}
            }
            else header('Location:/admin/siteInfo.html?nid='.$nid.'&msg=未选择图片，或图片文件大于10M');
        }
        //网站设置，上传关于图片
        if($_POST['action']=='uploadAboutImg'){
            if($_FILES['imgFile']['error']==0){
                require_once('/code/SiteInfo.php');
                $siteInfo=new SiteInfo();
                try{
                    $siteInfo->uploadImg('.'.$siteInfo->ABOUT_IMG_URL, $_FILES['imgFile']['tmp_name']);
                    header('Location:/admin/siteInfo.html?nid='.$nid.'&msg=关于图片上传成功');
                }
                catch(Exception $e){header('Location:/admin/siteInfo.html?msg='.$e->getMessage());}
            }
            else header('Location:/admin/siteInfo.html?nid='.$nid.'&msg=未选择图片，或图片文件大于10M');
        }
        //发送邮件
        if($_POST['action']=='sendEmail'){
            require_once('/code/SiteInfo.php');
            $siteInfo=new SiteInfo();
            $s=$siteInfo->getContact();
            $from=$_POST['email'];
            $to=$s['contact_email'];
            $subject='From: '.$_POST['name'].' Via: '.$s['site_name'];
            $message=$_POST['message']."\n\r\n\rVia: ".$s['site_name'];
            if(mail($to,$subject,$message,"From: $from")) header('Location:/contact/thankyou/index.html?sent=1');
            else header('Location:/contact/thankyou/index.html?sent=0');
        }
        //添加新闻
        if($_POST['action']=='addNews'){
            require_once('/code/News.php');
            if($_POST['caption']=='') header('Location:/admin/news.html?msg=请填写新闻标题');
            else{
                $news=new News();
                $n['is_active']=$_POST['is_active']=='on'?1:0;
                $n['caption']=$_POST['caption'];
                $n['content']='';
                $n['abstract']='';
                $nid=$news->addNews($n);
                if($nid>0) header('Location:/admin/editNews.html?nid='.$nid);
                else header('Location:/admin/news.html?msg=新闻添加失败');
            }
            
        }
        //编辑新闻
        if($_POST['action']=='editNews'){
            require_once('/code/News.php');
            $news=new News();
            $n['news_id']=$_POST['news_id'];
            $n['is_active']=$_POST['is_active']=='on'?1:0;
            $n['caption']=$_POST['caption'];
            $n['content']=$_POST['content'];
            $n['abstract']=$_POST['abstract'];
            if($nid=$news->editNews($n)) header('Location:/admin/news.html?msg=新闻已保存');
            else header('Location:/admin/news.html?msg=新闻未保存');
        }
        //删除新闻
        if($_POST['action']=='deleteNews'){
            require_once('/code/News.php');
            $news=new News();
            $nid=$_POST['news_id'];
            if($news->deleteNews($nid)) header('Location:/admin/news.html?msg=新闻已删除');
            else header('Location:/admin/news.html?msg=删除新闻失败');
        }
        //上传新闻图片
        if($_POST['action']=='uploadNewsImg'){
            $nid=$_POST['news_id'];
            if($_FILES['imgFile']['error']==0){
                require_once('/code/News.php');
                $news=new News();
                $imgFile=&$_FILES['imgFile'];
                try{
                    $news->uploadNewsImg($nid, $imgFile);
                    header('Location:/admin/editNews.html?nid='.$nid.'&msg=新闻图片上传成功');
                }
                catch(Exception $e){header('Location:/admin/editNews.html?nid='.$nid.'&msg='.$e->getMessage());}
            }
            else header('Location:/admin/editNews.html?nid='.$nid.'&msg=未选择图片，或图片文件大于10M');
        }
        //删除新闻图片
        if($_POST['action']=='deleteNewsImg'){
            require_once('/code/News.php');
            $news=new News();
            $nid=$_POST['news_id'];
            if($news->deleteNewsImg($nid)) header('Location:/admin/editNews.html?nid='.$nid.'&msg=新闻图片已删除');
            else header('Location:/admin/editNews.html?nid='.$nid.'&msg=新闻图片未删除');
        }
    }
    elseif(isset($_GET['action'])){

    }
    else{

    }///输入数字验证；
?>

